package com.netmi.myvideopicker.adapter;

import android.app.Activity;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.netmi.myvideopicker.BR;
import com.netmi.myvideopicker.R;
import com.netmi.myvideopicker.bean.VideoItem;
import com.netmi.myvideopicker.databinding.ActivityPlayVideoItemBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：ViewPager2的使用方式与recyclerview类似--使用databind绑定的方式
 * 创建人：tgw --暂时抛弃因为没找到好办法控制音频播放时机
 * 创建时间：2020/7/28
 * 修改备注：
 * 参考：https://www.jianshu.com/p/25aa5cacbfb9?utm_source=desktop&utm_medium=timeline
 *
 */

public class VideoPagerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<VideoItem> videos = new ArrayList<>();
    private Activity mActivity;

    //设置EXOplayer播放参数
    //1.默认的播放View
    private PlayerView playerView;
    //    private PlayerControlView playerControlView;//另一个播放界面
    //2.您可以ExoPlayer使用创建实例ExoPlayerFactory。工厂提供了一系列方法来创建ExoPlayer具有不同
    //级别自定义的实例。对于绝大多数用例，ExoPlayerFactory.newSimpleInstance应该使用其中一种 方法。
    //这些方法返回SimpleExoPlayer
    private SimpleExoPlayer player = null;
    // Produces DataSource instances through which media data is loaded.--生成加载媒体数据的数据源实例。
    private DataSource.Factory dataSourceFactory = null;
    // This is the MediaSource representing the media to be played.---这是表示要播放的媒体的媒体源
    private MediaSource videoSource = null;


    public VideoPagerAdapter(Activity mActivity) {
        this.mActivity = mActivity;

    }


    public VideoPagerAdapter(List<VideoItem> videos, Activity mActivity) {

        this.videos = videos;
        this.mActivity = mActivity;
    }


    public void refreshData(List<VideoItem> videos) {
        if (videos.size() > 0) {
            this.videos.clear();
            if (videos != null)
                this.videos.addAll(videos);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public VideoPagerAdapter.VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.activity_play_video_item, parent, false);
        return new VideoPagerAdapter.VideoViewHolder(viewDataBinding);
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VideoPagerAdapter.VideoViewHolder) {
            VideoPagerAdapter.VideoViewHolder videoViewHolder = (VideoPagerAdapter.VideoViewHolder) holder;
            ActivityPlayVideoItemBinding binding = (ActivityPlayVideoItemBinding) videoViewHolder.getBinding();
            binding.setVariable(BR.item, videos.get(position));
            creatVideoPlayer(mActivity,binding);
            prepareVideoSourse(videos.get(position).path);
            binding.executePendingBindings();
        }


    }


    @Override
    public int getItemCount() {
        return videos != null && videos.size() > 0 ? videos.size() : 0;
    }


    private void creatVideoPlayer(Activity context,ActivityPlayVideoItemBinding binding) {
        if (player != null){
            player = null;
        }
        player = new SimpleExoPlayer.Builder(context).build();
        // Bind the player to the view.
        binding.playerView.setPlayer(player);
        // Produces DataSource instances through which media data is loaded.
        dataSourceFactory = new DefaultDataSourceFactory(context,
                Util.getUserAgent(context, "myvideopicker"));
    }

    private void prepareVideoSourse(String videoAddress) {
        Uri mp4VideoUri = Uri.parse(videoAddress);
        player.setRepeatMode(Player.REPEAT_MODE_ONE);//无限循环
        // This is the MediaSource representing the media to be played.
        videoSource =
                new ProgressiveMediaSource.Factory(dataSourceFactory)
                        .createMediaSource(mp4VideoUri);
        // Prepare the player with the source.
        player.prepare(videoSource);
        player.setPlayWhenReady(false);//控制播放暂停开始

    }



    private class VideoViewHolder extends RecyclerView.ViewHolder {

        private ViewDataBinding binding;

        public VideoViewHolder(@NonNull ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }
    }
}